// 立即执行匿名函数
// 添加任务
// 展示任务
// 修改状态
// 删除
// 代码逻辑
// 数据部分：增、删除、修改=>函数
// 页面渲染部分
import TodoData from './module/dataProcessing'
import TodoDom from './module/domProcessing'
((document)=>{
    const Inp:HTMLInputElement = document.querySelector(".inp")
    const addBtn:HTMLButtonElement = document.querySelector(".todolist-add")
    const parent:HTMLElement = document.querySelector(".content")
    const data:TodoData = new TodoData([
        {
            id:1,
            content:"杨智宇是个快乐的小2b",
            completed:false
        }
    ],parent)
 
    function init():void {
        // 视图初始化
        // 事件处理
        handleEvent()
    }
    function handleEvent():void {
        addBtn.addEventListener('click', addClick, false)
        parent.addEventListener("click",listClick,false)
    }
    function listClick(e:MouseEvent):void{
        const target = e.target as HTMLElement;
        const tagName = target.tagName
        const id = Number(target.dataset.id)
        switch(tagName){
            case "INPUT":
                data.changeData(id,target)
                break;
            case "BUTTON":
                data.removeData(id,target)
                break;
            default:
                break;
        }
    }
    function addClick():void{
        const val:string = Inp.value.trim()
        if(val.length > 0) {
            // 处理数据
            let res = data.addData({
                id:data.todolist.length + 1,
                content:val,
                completed:false
            })
            if(res === 0){
                console.log('已存在')
            }else{
                data.addDom({
                    id:data.todolist.length,
                    content:val,
                    completed:false
                })
            }
        }
        Inp.value=""
    }
    init()
})(document)